import React, { Component } from 'react';
import { Form, Row, Col, Icon } from 'antd';
import ShowType from '../../../routes/common/showType';
import * as commonFunc from '../../Common/commonFunc';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sNo: '', /* 编号 */
      sType: '', /* 类型 */
      dLength: '', /* 长度 */
      dMaterialsLength: '', /* 材料长度 */
      dMachineLength: '', /* 上机长 */
      sName: '', /* 名称 */
      dWidth: '', /* 对应宽度 */
      dMaterialsWidth: '', /* 开料宽幅 */
      dMachineWidth: '', /* 上机宽幅 */
      iOrder: '', /* 序号 */
      dMaterialsKQty: '', /* 开数 */
      dTypesettingQty: '', /* 拼版数 */
      reserveContent: '',
      expand: false,
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/siscommonstyle', returnType: 'data',
        },
      });
      /* 获取主表数据 */
    } else if (sId === '' && this.props.newIdFlag) {
      const url = `${config.server_host}getUuid/${this.props.newIdlength}`;
      dispatch({
        type: 'content/getCopyDataId',
        payload: {
          sTabId, url, formRoute: '/siscommonstyle', componentName,
        },
      });
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sno'))[0],
        sType: mainConfig.filter(item => (item.sName.toLowerCase() === 'stype'))[0],
        dLength: mainConfig.filter(item => (item.sName.toLowerCase() === 'dlength'))[0],
        dMaterialsLength: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmaterialslength'))[0],
        dMachineLength: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmachinelength'))[0],
        sName: mainConfig.filter(item => (item.sName.toLowerCase() === 'sname'))[0],
        dWidth: mainConfig.filter(item => (item.sName.toLowerCase() === 'dwidth'))[0],
        dMaterialsWidth: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmaterialswidth'))[0],
        dMachineWidth: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmachinewidth'))[0],
        iOrder: mainConfig.filter(item => (item.sName.toLowerCase() === 'iorder'))[0],
        dMaterialsKQty: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmaterialskqty'))[0],
        dTypesettingQty: mainConfig.filter(item => (item.sName.toLowerCase() === 'dtypesettingqty'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      sId, disabled, content, dataSet,
    } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    this.setState({ disabled, dataList: dataSet });
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
      this.setState({ sId });
    }
    const mainConfig = this.state.masterConfig.gdsconfigformslave;
    if (Object.keys(mainConfig).length > 0) {
      const { sActiveId, app, form } = nextProps;
      const childProps = {
        app, form, sActiveId, sId, dataList, disabled,
      };
      const reserveContent = commonFunc.Reserve(mainConfig, childProps);
      this.setState({ reserveContent });
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  handleToggle = () => {
    const { expand } = this.state;
    this.setState({ expand: !expand });
  };

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, handleSelectChange, onClose,
    } = this.props;
    const {
      sNo, sType, dLength, dMaterialsLength, dMachineLength,
    } = this.state;
    const {
      sName, dWidth, dMaterialsWidth, dMachineWidth,
    } = this.state;
    const { iOrder, dMaterialsKQty, dTypesettingQty } = this.state;
    const {
      dataList, sId, sActiveId, disabled, reserveContent, expand,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      handleSelectChange,
      dataName,
      dataType,
      onClose,
    };
    const sNoProps = { showConfig: sNo, ...childProps };
    const sTypeProps = { showConfig: sType, ...childProps };
    const dLengthProps = { showConfig: dLength, ...childProps };
    const dMaterialsLengthProps = { showConfig: dMaterialsLength, ...childProps };
    const dMachineLengthProps = { showConfig: dMachineLength, ...childProps };
    const sNameProps = { showConfig: sName, ...childProps };
    const dWidthProps = { showConfig: dWidth, ...childProps };
    const dMaterialsWidthProps = { showConfig: dMaterialsWidth, ...childProps };
    const dMachineWidthProps = { showConfig: dMachineWidth, ...childProps };
    const iOrderProps = { showConfig: iOrder, ...childProps };
    const dMaterialsKQtyProps = { showConfig: dMaterialsKQty, ...childProps };
    const dTypesettingQtyProps = { showConfig: dTypesettingQty, ...childProps };
    return (
      <FormItem className={styles.searchMainForm}>
        <Row type="flex" style={{ height: expand ? 'auto' : '100px', overflow: 'hidden' }}>
          <Col
            span={6}
            order={sNo.iOrder > 24 ? 24 : sNo.iOrder}
            style={{ display: sNo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sNo" showDefaultName="编号" {...sNoProps} />
          </Col>
          <Col
            span={6}
            order={sType.iOrder > 24 ? 24 : sType.iOrder}
            style={{ display: sType.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sType" showDefaultName="类型" {...sTypeProps} />
          </Col>
          <Col
            span={6}
            order={dLength.iOrder > 24 ? 24 : dLength.iOrder}
            style={{ display: dLength.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dLength" showDefaultName="长度" {...dLengthProps} />
          </Col>
          <Col
            span={6}
            order={dMaterialsLength.iOrder > 24 ? 24 : dMaterialsLength.iOrder}
            style={{ display: dMaterialsLength.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMaterialsLength" showDefaultName="材料长度" {...dMaterialsLengthProps} />
          </Col>
          <Col
            span={6}
            order={dMachineLength.iOrder > 24 ? 24 : dMachineLength.iOrder}
            style={{ display: dMachineLength.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMachineLength" showDefaultName="上机长度" {...dMachineLengthProps} />
          </Col>
          <Col
            span={6}
            order={sName.iOrder > 24 ? 24 : sName.iOrder}
            style={{ display: sName.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sName" showDefaultName="名称" {...sNameProps} />
          </Col>
          <Col
            span={6}
            order={dWidth.iOrder > 24 ? 24 : dWidth.iOrder}
            style={{ display: dWidth.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dWidth" showDefaultName="对应宽度" {...dWidthProps} />
          </Col>
          <Col
            span={6}
            order={dMaterialsWidth.iOrder > 24 ? 24 : dMaterialsWidth.iOrder}
            style={{ display: dMaterialsWidth.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMaterialsWidth" showDefaultName="开料宽幅" {...dMaterialsWidthProps} />
          </Col>
          <Col
            span={6}
            order={dMachineWidth.iOrder > 24 ? 24 : dMachineWidth.iOrder}
            style={{ display: dMachineWidth.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMachineWidth" showDefaultName="上机宽幅" {...dMachineWidthProps} />
          </Col>
          <Col
            span={6}
            order={iOrder.iOrder > 24 ? 24 : iOrder.iOrder}
            style={{ display: iOrder.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="iOrder" showDefaultName="序号" {...iOrderProps} />
          </Col>
          <Col
            span={6}
            order={dMaterialsKQty.iOrder > 24 ? 24 : dMaterialsKQty.iOrder}
            style={{ display: dMaterialsKQty.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMaterialsKQty" showDefaultName="开数" {...dMaterialsKQtyProps} />
          </Col>
          <Col
            span={6}
            order={dTypesettingQty.iOrder > 24 ? 24 : dTypesettingQty.iOrder}
            style={{ display: dTypesettingQty.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dTypesettingQty" showDefaultName="拼版数" {...dTypesettingQtyProps} />
          </Col>
          {reserveContent}
        </Row>
        <button className={styles.collapseA} onClick={this.handleToggle}>
          <span>{expand ? '缩略展示' : '显示全部'}</span><Icon type={expand ? 'up' : 'down'} />
        </button>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
